package com.pengbei.enshrine.common.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pengbei.enshrine.R;

public class ToastUtils {
    private static Context mContext;
    private static ToastUtils toastUtils = null;

    public static ToastUtils init(Context context) {
        if (null == toastUtils) {
            toastUtils = new ToastUtils();
            mContext = context;
        }

        return toastUtils;
    }

    /**
     * 自定义icon
     *
     * @param message
     * @param res
     */
    public static void showCustomIcon(String message, int res) {
        showToast(message, Toast.LENGTH_SHORT, res, true);
    }

    /**
     * 警告消息
     *
     * @param message
     */
    public static void showWarn(String message) {
        showToast(message, Toast.LENGTH_SHORT, R.drawable.vector_warning, true);
    }

    /**
     * 错误消息
     *
     * @param message
     */
    public static void showError(String message) {
        showToast(message, Toast.LENGTH_SHORT, R.drawable.vector_error, true);
    }

    /**
     * 成功消息
     *
     * @param message
     */
    public static void showSuccess(String message) {
        showToast(message, Toast.LENGTH_SHORT, R.drawable.vector_success, true);
    }

    /**
     * 不显示icon
     *
     * @param message
     */
    public static void showNoIcon(String message) {
        showToast(message, Toast.LENGTH_SHORT, 0, false);
    }


    private static void showToast(String message, int duration, int res, boolean visibleIcon) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.toast_common, null);
        TextView mTextView = (TextView) rootView.findViewById(R.id.toast_text);
        ImageView icon = (ImageView) rootView.findViewById(R.id.ivTip);
        mTextView.setText(message);
        Toast toastStart = new Toast(mContext);
        //获取屏幕高度
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        toastStart.setGravity(Gravity.CENTER, 0, 0);
        toastStart.setDuration(duration);
        if (!visibleIcon) {
            icon.setVisibility(View.GONE);
        } else {
            icon.setImageResource(res);
        }

        toastStart.setView(rootView);
        toastStart.show();
    }
}
